package com.ddxz.hetong.service.impl;

import com.ddxz.hetong.dto.RongZiDto;
import com.ddxz.hetong.dto.DeviceDeptDto;
import com.ddxz.hetong.mapper.DeviceMapper;
import com.ddxz.hetong.mapper.ProjectMapper;
import com.ddxz.hetong.mapper.RongZiHeTongMapper;
import com.ddxz.hetong.service.HeTongService;
import com.ddxz.tool.web.dto.DdxzResponse;
import com.ddxz.tool.web.dto.PageResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ddxz.hetong.enums.HeTongConst.*;
import static com.ddxz.hetong.enums.HeTongErrorStatus.INVALID_STATUS;
import static com.ddxz.hetong.enums.HeTongErrorStatus.INVALID_TYPE;

/**
 * 业务逻辑层，相关业务逻辑在本类中进行处理
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2022/7/7
 */

@Service
public class HeTongServiceImpl implements HeTongService {
    /**
     * 获取所有合同的总额
     *
     * @param type 合同类型
     * @return 合同总额
     */
    @Override
    public DdxzResponse getTotalMoney(Integer type) {
        switch (type) {
            case RONG_ZI:
                return DdxzResponse.correct(rzMapper.getTotalRongZiMoney());
            case GONG_CHENG:
                return DdxzResponse.correct(proMapper.getTotalMoney());
            case SHE_BEI:
                return DdxzResponse.correct(devMapper.getTotalMoney());
            default:
                return DdxzResponse.error(INVALID_TYPE);
        }
    }

    /**
     * 获取贷款结构列表
     *
     * @param status   状态信息
     * @param deptName 单位名称
     * @param year     年份信息
     * @param pageSize  每页大小
     * @param pageNum 页码
     * @return 贷款机构列表
     */
    @Override
    public DdxzResponse getDeptList(Integer status, String deptName, Integer year, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        Page<? extends Object> dto;
        switch (status) {
            case ONLY_DEPT:
                dto = rzMapper.getDeptList();
                break;
            case ONLY_START_YEAR:
                dto = rzMapper.getYearList();
                break;
            case START_YEAR_BY_DEPT:
                dto = rzMapper.getYearByDept(deptName);
                break;
            case DEPT_BY_START_YEAR:
                dto = rzMapper.getDeptByStartYear(year);
                break;
            case START_YEAR_AND_DEPT:
                dto = rzMapper.getDetail(deptName, year);
                break;
            case ONLY_END_YEAR:
                dto = rzMapper.getEndYearList();
                break;
            case END_YEAR_BY_DEPT:
                dto = rzMapper.getEndYearByDept(deptName);
                break;
            case END_YEAR_AND_DEPT:
                dto = rzMapper.getDetailList(deptName, year);
                break;
            case DEPT_BY_END_YEAR:
                dto = rzMapper.getDeptByEndYear(year);
                break;
            case MONTH_BY_START_YEAR_DEPT:
                dto = rzMapper.getMonthByStartYearAndDept(year, deptName);
                break;
            case MONTH_BY_END_YEAR_DEPT:
                dto = rzMapper.getMonthByEndYearAndDept(year, deptName);
                break;
            case MONTH_BY_START_YEAR:
                dto = rzMapper.getMonthByStartYear(year);
                break;
            case MONTH_BY_END_YEAR:
                dto = rzMapper.getMonthByEndYear(year);
                break;
            default:
                return DdxzResponse.error(INVALID_STATUS);
        }

        PageResponse rst = new PageResponse(dto.getResult().size());
        rst.setList(dto.getResult());
        rst.setTotalPage(dto.getPages());
        rst.setTotalElements(dto.getTotal());

        return DdxzResponse.correct(rst);
    }

    /**
     * 获取单位的发放比例
     *
     * @param type 预留
     * @return 贷款结构的发放比例
     */
    @Override
    public DdxzResponse getDaptPercent(Integer type) {
        List<RongZiDto> dto = rzMapper.getDeptPercent();
        return DdxzResponse.correct(dto);
    }

    /**
     * 获取单位/合同列表信息
     *
     * @param status    控制状态
     * @param deptName  单位名称
     * @param protoName 合同名称
     * @param pageSize  页面大小
     * @param pageNum   页面
     * @return 列表
     */
    @Override
    public DdxzResponse getDeviceDeptList(Integer status, String deptName, String protoName, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        Page<? extends Object> dto;
        switch (status) {
            case ONLY_DEV_DEPT:
                dto = devMapper.getDeptList();
                break;
            case DEV_DEPT_PROTO:
                dto = devMapper.getDeptProtoList(deptName, protoName);
                break;
            case DEV_DETAIL:
                dto = devMapper.getDetail(deptName, protoName);
                break;
            default:
                return DdxzResponse.error(INVALID_STATUS);
        }

        PageResponse rst = new PageResponse(dto.getResult().size());
        rst.setTotalElements(dto.getTotal());
        rst.setTotalPage(dto.getPages());
        rst.setList(dto.getResult());
        return DdxzResponse.correct(rst);
    }

    /**
     * 获取工程/合同的统计数据或详情数据
     *
     * @param status   控制状态
     * @param deptName 单位名称
     * @param protoName 合同名称
     * @param year     年份
     * @param month 月份
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @return 列表
     */
    @Override
    public DdxzResponse getProjectList(Integer status, String deptName, String protoName, Integer year, String month, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<? extends Object> dto;
        switch (status) {
            case PROJECT_ONLY_DEPT:
                dto = proMapper.getDeptList();
                break;
            case PROJECT_ONLY_YEAR:
                dto = proMapper.getYearList();
                break;
            case PROJECT_DEPT_BY_YEAR:
                dto = proMapper.getDeptByYear(year);
                break;
            case YEAR_BY_PROJECT_DEPT:
                dto = proMapper.getYearByDept(deptName);
                break;
            case MONTY_BY_YEAR:
                dto = proMapper.getMonthByYear(year);
                break;
            case MONTY_BY_YEAR_DEPT:
                dto = proMapper.getMonthByDeptYear(deptName, year);
                break;
            case PROJECT_DETAIL:
                dto = proMapper.getDetail(deptName, year, month, protoName);
                break;
            case PROJECT_ONLY_PROTO:
                dto = proMapper.getProtoList();
                break;
            default:
                return DdxzResponse.error(INVALID_STATUS);
        }

        PageResponse rst = new PageResponse(dto.getResult().size());
        rst.setTotalElements(dto.getTotal());
        rst.setTotalPage(dto.getPages());
        rst.setList(dto.getResult());
        return DdxzResponse.correct(rst);
    }

    /**
     * 根据进度，获取付款详情
     *
     * @param status   控制状态
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @return 列表
     */
    @Override
    public DdxzResponse getDevicePercentDetail(Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<DeviceDeptDto> dto = devMapper.getPercentDetail(status);

        PageResponse rst = new PageResponse(dto.getResult().size());
        rst.setTotalElements(dto.getTotal());
        rst.setTotalPage(dto.getPages());
        rst.setList(dto.getResult());
        return DdxzResponse.correct(rst);
    }

    /**
     * 获取不同进度下的合同数
     *
     * @return 列表
     */
    @Override
    public DdxzResponse getDevicePercentList() {
        List<DeviceDeptDto> list = devMapper.getPercentList();
        return DdxzResponse.correct(list);
    }

    /**
     * 获取设备类各种合同的统计指标
     *
     * @return 列表
     * @param pageNum 页码
     * @param pageSize 页面大小
     */
    @Override
    public DdxzResponse getDeviceProtoList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<DeviceDeptDto> dto = devMapper.getProtoList();
        PageResponse rst = new PageResponse(dto.getResult().size());
        rst.setTotalElements(dto.getTotal());
        rst.setTotalPage(dto.getPages());
        rst.setList(dto.getResult());
        return DdxzResponse.correct(rst);
    }

    private final RongZiHeTongMapper rzMapper;

    private final DeviceMapper devMapper;

    private final ProjectMapper proMapper;

    public HeTongServiceImpl(RongZiHeTongMapper rzMapper, DeviceMapper devMapper, ProjectMapper proMapper) {
        this.rzMapper = rzMapper;
        this.devMapper = devMapper;
        this.proMapper = proMapper;
    }
}
